package com.lyt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lyt.Annotation.LoginToken;
import com.lyt.Annotation.PassToken;
import com.lyt.bean.LoginBody;
import com.lyt.bean.User;
import com.lyt.service.TokenService;
import com.lyt.service.UserService;
import com.lyt.util.AesUtil;
import org.apache.commons.codec.DecoderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: lyt
 * @Date: 2023/04/09/20:03
 * @Description:
 */
@RestController
public class LoginJwt {
    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;

    @CrossOrigin
    @PassToken
    @PostMapping("login")
    public Object login(String encStr) {
        System.out.println("传进来的秘密"+encStr);
//        String passWord2= AesUtil.decrypt(password,"lyt");
//        System.out.println("解密后的密码passWord2:"+passWord2);


        String uniqueKey = "385f33cb91484b04a177828829081ab7";
        // 加密前数据，此处数据与前端数据一致(加密内容包含有时间戳)，请注意查看前端加密前数据
//        String src = "{\"username\":\"用户名\",\"password\":\"密码\",\"timestamp\":1628218094188}";
//        System.out.println("密钥:" + uniqueKey);
//        System.out.println("原字符串:" + src);
        // 加密
        String encrypt = null;
        LoginBody loginBody = null;
        try {
//            encrypt = AesUtil.encrypt(src, uniqueKey);
//            System.out.println("加密：" + encrypt);
            // 解密
            String decrypt = null;

            decrypt = AesUtil.decrypt(encStr, uniqueKey);
            System.out.println("解密：" + decrypt);
            loginBody = JSON.parseObject(decrypt,LoginBody.class);
            System.out.println("解密后的body"+loginBody);


        }catch (DecoderException | NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | InvalidKeyException | IllegalBlockSizeException | InvalidAlgorithmParameterException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        JSONObject jsonObject = new JSONObject();

        assert loginBody != null;

        User user = userService.getUser(loginBody.getUserId(),loginBody.getPassword());
        if (user == null) {
            jsonObject.put("message", "登录失败！");
            return jsonObject;
        } else {
            String token = tokenService.getToken(user);
            jsonObject.put("token", token);
//            jsonObject.put("user", user);
            return jsonObject;
        }
    }

    @LoginToken
    @PostMapping("/getMessage")
    public String getMessage() {

        return "你已通过验证";
    }

}
